//
//  GetPhoto.m
//  XJHD_Customer
//
//  Created by sdc on 15/9/8.
//  Copyright (c) 2015年 com.ync. All rights reserved.
//

#import "GetPhoto.h"

//#import "CreateUserViewController.h"

@interface GetPhoto ()
@property(nonatomic, assign)BOOL isCanEdit;
@end

@implementation GetPhoto

+ (instancetype)sharedInstance {
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    
    return sharedInstance;
}


#pragma mark 工具
-(void)showAlertMesssage:(NSString *)message{
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示:" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    
    [alertView show];
}



- (void)takePhotoWithController:(UIViewController *)controller andIsCanEdit:(BOOL)isCanEdit
{
    _isCanEdit = isCanEdit;
    //NSLog(@"照相");
    //1.照相
    //UIImagePickerController 作用:处理与照相,图片的存取相关的操作
    //1.1判断执行的操作是照相,1.2判断设备是否支持照相功能
    //UIImagePickerControllerSourceTypeCamera:照相功能类型
    
    //判断设备是否支持照相
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        [self loadImagePickerWithType:UIImagePickerControllerSourceTypeCamera andController:controller];
    } else {
        [self showAlertMesssage:@"此设备不支持照相"];
    }
    
//    BaseViewController * bb = [[BaseViewController alloc]init];
    
    
}


- (void)pickupPhotoWithController:(UIViewController *)controller andIsCanEdit:(BOOL)isCanEdit
{
    _isCanEdit = isCanEdit;
    [self loadImagePickerWithType:UIImagePickerControllerSourceTypePhotoLibrary andController:controller];
}


#pragma mark 照相,取图片
-(void)loadImagePickerWithType:(UIImagePickerControllerSourceType)type andController:(UIViewController *)controller{
    
    UIImagePickerController *pickerCon = [[UIImagePickerController alloc]init];
    
    pickerCon.navigationBar.barTintColor = [UIColor greenColor];
    pickerCon.navigationBar.tintColor = [UIColor whiteColor];
    [pickerCon.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:17],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //设置要执行的功能的类型
    pickerCon.sourceType = type;
    
    //执行功能允许进行后续操作
    pickerCon.allowsEditing = _isCanEdit;
    
    //设置代理
    pickerCon.delegate = self;
    
    //进入pickerCon控制器
    //注意:通常处理pickerCon相关的跳转的时,用模态视图跳转
    [controller presentViewController:pickerCon animated:YES completion:^{
        NSLog(@"跳转成功");
    }];
    
    
}

#pragma imagePickerDelegate
//点击choose时触发的方法
//info:里面装着跟选取的资源相关的内容
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //获取选取的资源的类型
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //确定资源的类型是图片
    //kUTTypeImage是系统设置好的表示资源类型的图片类型
    if ([type isEqualToString:(NSString *)kUTTypeImage]) {
        //取出选好的图片
        UIImage *image = nil;
        if (_isCanEdit) {
            image = [info objectForKey:UIImagePickerControllerEditedImage];
        }else{
            image = [info objectForKey:UIImagePickerControllerOriginalImage];
        }
        
        
        //        //通过重绘图片,对图片进行压缩
        //        UIImage *newImage = [[ImageTool shareTool] resizeImageToSize:CGSizeMake(100, 100) sizeOfImage:image];
        
        self.image(image);
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

//点击cencel按钮时触发的方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}




@end
